
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:reabampos/src/reabam/operation/rb_config.dart';
import 'package:reabampos/src/reabam/ui/setting/pagestate_setting_device.dart';
import 'package:reabampos/src/reabam/ui_self_service/db_download/pagestate_ss_db_download_progress.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/select_staff/pagestate_ss_select_staff_list.dart';
import 'package:reabampos/src/reabam/ui_self_service/mine/select_store/pagestate_ss_select_store_list.dart';
import 'package:reabampos/src/reabam/ui_self_service/pagestate_ss_login.dart';
import 'package:reabampos/src/reabam/utils/rb_color.dart';
import 'package:reabampos/src/reabam/utils/rb_widget_ss.dart';
import 'package:xsdk_flutter/xsdk/xsdk_flutter_package.dart';


/*
系统设置
 */
class PageStateSelfServiceSetting extends XPageState{

  Map<String,dynamic>? staff;

  PageStateSelfServiceSetting();


  @override
  void onStart() {

    setXTopBarWidget(
        height: null,
        child: RBWidgets_SelfService.getTitleBarWidget(
            this,
            title: '设置',
            rightBt: '退出登录',
            onTapRightBt: (){
              startPage(StatePageSelfServiceLogin(),cleanUntilRetainPageName: '/');
            }
        )
    );

    setXBottomBarWidget(
        height: 260,
        child: RBWidgets_SelfService.getBottomBarOneButton(
            text: '去收银',
            onPressed: () {
              finishPage();
            }
        )
    );
  }

  @override
  Widget getBodyWidget(BuildContext context) {
    String companyId=XSharePreferencesUtils.getString(RBConfigs.SP_companyId);
    String dbCacheTime=XSharePreferencesUtils.getString("XDownloadLastTimeReabamPos_DB$companyId");

    return XWidgetUtils.getLayoutSingleChildScrollView(
        child:  XWidgetUtils.getLayoutColumn(
            shrinkWrap:true,
            children: [
              RBWidgets_SelfService.getBaseInfoLeftRightWidget(
                  leftTextString: '账号管理',
                  rightWidget: XWidgetUtils.getLayoutRow(
                      mainAxisAlignment:MainAxisAlignment.end,
                      children: [
                        XWidgetUtils.getWidgetText(
                            XSharePreferencesUtils.getString(RBConfigs.SP_UserCode),
                            style: XStyleUtils.getTextStyle(
                              fontSize:34,
                              color: XColorUtils.color333333,
                              lineHeight: 1,
                            )
                        ),
                        XWidgetUtils.getXLayoutRoundedRect(
                          backgroundColor: RBColors.reabamColor4,
                          radius_bottomLeft: 5,
                          radius_bottomRight: 5,
                          radius_topRight: 5,
                          radius_topLeft: 5,
                          height: 40,
                          alignment: Alignment.center,
                          padding: XWidgetUtils.getEdgeInsets(10, 0, 10, 0),
                          margin: XWidgetUtils.getEdgeInsets(10, 0, 0, 0),
                          child: XWidgetUtils.getWidgetText(
                              XSharePreferencesUtils.getString(RBConfigs.SP_UserTypeName),
                              style: XStyleUtils.getTextStyle(
                                fontSize:28,
                                color: Colors.white,
                                lineHeight: 1,
                              )
                          ),
                        )
                      ]
                  )
              ),
              RBWidgets_SelfService.getCommonInfoTextWidget(
                  leftTextString: '当前门店',
                  rightTextValue: XSharePreferencesUtils.getString(RBConfigs.SP_CompanyName),
                onTap: (){
                    //选择门店
                  startPage(
                      PageStateSelfServiceSelectStoreList(),
                      resultCallback: (res){
                        if(res!=null){
                          updateData();
                        }
                      }
                  );
                }
              ),
              RBWidgets_SelfService.getCommonInfoTextWidget(
                  leftTextString: '订单默认导购',
                  rightTextValue: '${staff?['staffName']??""}',
                onTap: (){
                    //选择导购
                    startPage(
                        PageStateSelfServiceSelectStaffList(),
                      resultCallback: (res){
                          if(res!=null){
                            updateData();
                          }
                      }
                    );
                }
              ),
              RBWidgets_SelfService.getCommonInfoTextWidget(
                  leftTextString: '未上传订单',
                  rightTextValue: '0'
              ),
              RBWidgets_SelfService.getBaseInfoLeftRightWidget(
                  leftTextString: '商品缓存',
                  rightWidget: XWidgetUtils.getLayoutRow(
                      children: [
                        XWidgetUtils.getLayoutExpanded(
                            XWidgetUtils.getWidgetText(
                                XStringUtils.isEmpty(dbCacheTime)?'':'(最近更新时间: $dbCacheTime)',
                                style: XStyleUtils.getTextStyle(
                                  fontSize:28,
                                  color: XColorUtils.color999999,
                                  lineHeight: 1,
                                )
                            )
                        ),
                        XWidgetUtils.getWidgetGestureDetector(
                            child: XWidgetUtils.getXLayoutRoundedRectWithBorder(
                                width: 180,
                                height: 50,
                                alignment: Alignment.center,
                                borderColor: RBColors.reabamColor4,
                                borderWidth: XTempData.defaultDividerHeight,
                                child: XWidgetUtils.getWidgetText(
                                    '下载数据',
                                    style: XStyleUtils.getTextStyle(
                                      fontSize: 28,
                                      color: RBColors.reabamColor4,
                                      lineHeight: 1,
                                    )
                                )
                            ),
                          onTap: (){
                              startPage(PageStateSelfServiceDownloadDBProgress());
                          }
                        )
                      ]
                  )
              ),
              RBWidgets_SelfService.getCommonInfoTextWidget(
                  leftTextString: '系统版本',
                  rightTextValue: 'V${RBConfigs.versionName}',
                  isShowRightArrow: false
              ),
              RBWidgets_SelfService.getCommonInfoTextWidget(
                  leftTextString: '连接设备',
                  rightTextValue: '',
                rightTextHintText: '',
                onTap: () async {
                  try{
                    RBConfigs.localIP=await XWifiUtils.getWifiIP();
                    XLogUtils.printLog('---localIP=${RBConfigs.localIP}');

                    startPage(
                        DeviceSettingStatePage(),
                        resultCallback: (dynamic result){
                          if(result!=null){
                            //更新设备记录设置
                            XNotifyStateChangeUtils.notifyStateChange(
                                action: RBConfigs.Notify_Global_Handler,
                                key: 'initAllDevicesNoteSetting',
                                value: ''
                            );
                          }
                        }
                    );
                  }catch(e){
                    XLogUtils.printLog('$e');
                  }
                }
              ),
            ]
        )
    );
  }


  @override
  Color getXPageBackgroundColor() {
    return Colors.white;
  }



  @override
  void onResumed() {
    super.onResumed();
    setState(() {

    });
  }


  @override
  Future<void> updateData({int pageIndex=1}) async {

    setState(() {
      String staffJson=XSharePreferencesUtils.getString(RBConfigs.SP_staff);
      XLogUtils.printLog('---staffJson=$staffJson');
      staff=XJsonUtils.json2Map(staffJson);
    });


  }




}